﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MINERP.COM;

namespace MINERP.BLL.GoodsDelivery
{
    public class DeliverFindBLL
    {
        private DAL.GoodsDelivery.DeliverFindDAL dal = new DAL.GoodsDelivery.DeliverFindDAL();

        #region 获取所有的商户集合
        /// <summary>
        /// 获取所有的商户集合
        /// </summary>
        /// <returns></returns>
        public List<MODEL.GoodsDelivery.ShopNormal> GetShopList()
        {
            try
            {
                DataTable dt = dal.GetShopDT();
                List<MODEL.GoodsDelivery.ShopNormal> shops = ChangeShopDtToList(dt);
                return shops;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 将商户从DT转化成List
        /// <summary>
        /// 将商户从DT转化成List
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        private List<MODEL.GoodsDelivery.ShopNormal> ChangeShopDtToList(DataTable dt)
        {
            try
            {
                List<MODEL.GoodsDelivery.ShopNormal> shops = new List<MODEL.GoodsDelivery.ShopNormal>();
                MODEL.GoodsDelivery.ShopNormal s = new MODEL.GoodsDelivery.ShopNormal();
                foreach (DataRow r in dt.Rows)
                {
                    MODEL.GoodsDelivery.ShopNormal shop = new MODEL.GoodsDelivery.ShopNormal();
                    shop.ShopId = Convert.ToInt32(r["ShopId"]);
                    shop.ShopName = r["ShopName"].ToString();
                    shops.Add(shop);
                }
                return shops;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        public List<MODEL.GoodsDelivery.DeliveryBack> GetDeliveryBillByShopId(int shopId, DateTime startTime, DateTime endTime, string ctmName, string orderProductCode, int status)
        {
            try
            {
                int noPrint = (int)DeliveryStatus.NoPrint;
                int printed = (int)DeliveryStatus.Printed;
                int deliverred = (int)DeliveryStatus.Deliverred;
                List<MODEL.GoodsDelivery.DeliveryBack> AllBills = new List<MODEL.GoodsDelivery.DeliveryBack>();     //该商户所有的账单
                if (status == noPrint || status == -1)                  //-1表示全部
                {
                    //1 还未打印的账单：1从shop_bill中获取的还未分配到发货格子的部分 2从DeliveryInfo中获取的已分配到发货格子的部分
                    DataTable dtShopBill = dal.GetNoDistributeBill(shopId);
                    DataTable dtNoPrint = dal.GetNoPrintBill(shopId, noPrint);
                    AllBills.AddRange(ChangePartDtToList(dtShopBill, noPrint));
                    AllBills.AddRange(ChangePartDtToList(dtNoPrint, noPrint));
                }
                if (status == printed || status == -1)
                {
                    //2 正在发货
                    DataTable dtPrinted = dal.GetPrintedBill(shopId, printed);
                    //AllBills.AddRange(ChangePartDtToList(dtPrinted, printed));
                    AllBills.AddRange(ChangeDeliverredDtToList(dtPrinted, printed));
                }
                if (status == deliverred || status == -1)
                {
                    //3 已发货
                    DataTable dtDeliverred = dal.GetDeliverredBill(shopId, deliverred, startTime, endTime);
                    AllBills.AddRange(ChangeDeliverredDtToList(dtDeliverred, deliverred));
                }

                //如果条件包括客户名、产品编号，要进一步筛选
                if (ctmName.Length > 0 || orderProductCode.Length > 0)
                {
                    List<int> BillIds = new List<int>();
                    foreach (MODEL.GoodsDelivery.DeliveryBack bill in AllBills)
                    {
                        BillIds.Add(bill.BillId);
                    }
                    BillIds = dal.FilterBill(BillIds, ctmName, orderProductCode);
                    List<MODEL.GoodsDelivery.DeliveryBack> AllBills2 = new List<MODEL.GoodsDelivery.DeliveryBack>();
                    foreach (int billId in BillIds)
                    {
                        foreach (MODEL.GoodsDelivery.DeliveryBack bill in AllBills)
                        {
                            if (billId == bill.BillId)
                            {
                                AllBills2.Add(bill);
                                break;
                            }
                        }
                    }
                    return AllBills2;
                }
                return AllBills;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 排除商户，按其他条件查询
        /// </summary>
        /// <param name="startTime"></param>
        /// <param name="ctmName"></param>
        /// <param name="orderProductCode"></param>
        /// <returns></returns>
        public List<MODEL.GoodsDelivery.DeliveryBack> GetDeliveryBillByShopId(DateTime startTime, DateTime endTime, string ctmName, string orderProductCode, int status)
        {
            try
            {
                int noPrint = (int)DeliveryStatus.NoPrint;
                int printed = (int)DeliveryStatus.Printed;
                int deliverred = (int)DeliveryStatus.Deliverred;

                if (ctmName.Length == 0 && orderProductCode.Length == 0 && status == noPrint)
                    return null;
      
                List<MODEL.GoodsDelivery.DeliveryBack> bills = new List<MODEL.GoodsDelivery.DeliveryBack>();
                if (status == noPrint || status == -1)              //-1表示全部发货状态
                {
                    //1 还未打印的账单：1从shop_bill中获取的还未分配到发货格子的部分 2从DeliveryInfo中获取的已分配到发货格子的部分
                    DataTable dtShopBill = dal.GetNoDistributeBill(ctmName, orderProductCode);
                    DataTable dtNoPrint = dal.GetNoPrintBill(ctmName, orderProductCode, noPrint);
                    bills.AddRange(ChangePartDtToList(dtShopBill, noPrint));
                    bills.AddRange(ChangePartDtToList(dtNoPrint, noPrint));
                }
                if (status == printed || status == -1)
                {
                    //2 正在发货
                    DataTable dtPrinted = dal.GetPrintedBill(ctmName, orderProductCode, printed);
                    bills.AddRange(ChangeDeliverredDtToList(dtPrinted, printed));
                }
                if (status == deliverred || status == -1)
                {
                    //3 已发货
                    DataTable dtDeliverred = dal.GetDeliverredBill(ctmName, orderProductCode, deliverred, startTime, endTime);
                    bills.AddRange(ChangeDeliverredDtToList(dtDeliverred, deliverred));
                }
                return bills;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        private List<MODEL.GoodsDelivery.DeliveryBack> ChangePartDtToList(DataTable dt, int status)
        {
            try
            {
                List<MODEL.GoodsDelivery.DeliveryBack> list = new List<MODEL.GoodsDelivery.DeliveryBack>();
                foreach (DataRow r in dt.Rows)
                {
                    MODEL.GoodsDelivery.DeliveryBack b = new MODEL.GoodsDelivery.DeliveryBack();
                    b.BillId = Convert.ToInt32(r["BillId"]);
                    b.BillOrderCode = Convert.ToInt32(r["BillOrderCode"]);
                    b.ShopId = Convert.ToInt32(r["ShopId"]);
                    b.ShopName = r["ShopName"].ToString();
                    switch (status)
                    {
                        case 0:
                            b.Status = "还未打印发货单"; break;
                        case 1:
                            b.Status = "正在发货"; break;
                        case 2:
                            b.Status = "已发货"; break;
                    }
                    list.Add(b);
                }
                return list;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private List<MODEL.GoodsDelivery.DeliveryBack> ChangeDeliverredDtToList(DataTable dt, int status)
        {
            try
            {
                List<MODEL.GoodsDelivery.DeliveryBack> list = new List<MODEL.GoodsDelivery.DeliveryBack>();
                foreach (DataRow r in dt.Rows)
                {
                    MODEL.GoodsDelivery.DeliveryBack b = new MODEL.GoodsDelivery.DeliveryBack();
                    if (r["ArrivalTime"] != DBNull.Value)
                        b.ArrivalTime = Convert.ToDateTime(r["ArrivalTime"]);
                    b.BillId = Convert.ToInt32(r["BillId"]);
                    b.BillOrderCode = Convert.ToInt32(r["BillOrderCode"]);
                    if (r["DeliverId"] != DBNull.Value)
                    {
                        b.DeliverId = Convert.ToInt16(r["DeliverId"]);
                    }
                    if (r["TuoYunDanCode"] != DBNull.Value)
                        b.TuoYunDanCode = r["TuoYunDanCode"].ToString();
                    if (r["Num"] != DBNull.Value)
                        b.Num = Convert.ToInt16(r["Num"]);
                    if (r["DeliverObject"] != DBNull.Value)
                        b.DeliverObject = r["DeliverObject"].ToString();
                    if (r["DeliverTime"] != DBNull.Value)
                        b.DeliverTime = Convert.ToDateTime(r["DeliverTime"]);
                    if (r["OtherMoney"] != DBNull.Value)
                        b.OtherMoney = Convert.ToDouble(r["OtherMoney"]);
                    if (r["Remark"] != DBNull.Value)
                        b.Remark = r["Remark"].ToString();
                    if (r["SelfMoney"] != DBNull.Value)
                        b.SelfMoney = Convert.ToDouble(r["SelfMoney"]);
                    b.ShopId = Convert.ToInt32(r["ShopId"]);
                    if (r["ShopMoney"] != DBNull.Value)
                        b.ShopMoney = Convert.ToDouble(r["ShopMoney"]);
                    b.ShopName = r["ShopName"].ToString();
                    if (r["StationRemark"] != DBNull.Value)
                        b.StationRemark = r["StationRemark"].ToString();
                    if (r["ReceiveStationRemark"] != DBNull.Value)
                    {
                        b.ReceiveStationRemark = r["ReceiveStationRemark"].ToString();
                    }
                    if (r["username"] != DBNull.Value)
                    {
                        b.UserName = r["username"].ToString();
                    }
                    if (status == 1)
                        b.Status = "正在发货";
                    else
                        b.Status = "已发货";
                    list.Add(b);
                }
                return list;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 账单内的产品
        /// </summary>
        /// <param name="billId"></param>
        /// <returns></returns>
        public List<OrderInfoCL> GetOrderProductByBillId(int billId)
        {
            try
            {
                DataTable dt = dal.GetOrderInfoByBillId(billId);
                List<OrderInfoCL> infos = new List<OrderInfoCL>();
                foreach (DataRow r in dt.Rows)
                {
                    OrderInfoCL info = new OrderInfoCL();
                    info.CtmName = r["CtmName"].ToString();
                    info.OrderProductCode = r["OrderInfoCode"].ToString();
                    info.OrderProductName = r["OrderProductName"].ToString();
                    infos.Add(info);
                }
                return infos;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #region 修改商户邮箱
        /// <summary>
        /// 修改商户邮箱
        /// </summary>
        /// <param name="shopId"></param>
        /// <param name="mailAddr"></param>
        public void UpdateShopMail(int shopId, string mailAddr)
        {
            try
            {
                dal.UpdateShopMail(shopId, mailAddr);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 商户的email地址
        /// <summary>
        /// 商户的email地址
        /// </summary>
        /// <param name="shopId"></param>
        /// <returns></returns>
        public string GetShopMailAddr(int shopId)
        {
            try
            {
                return dal.GetShopMailAddr(shopId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        //-----------------------------数据结构---------------------
        public class OrderInfoCL
        {
            public string CtmName { get; set; }
            public string OrderProductCode { get; set; }
            public string OrderProductName { get; set; }
        }

        public DataTable CreateOutDt()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("CtmName", typeof(string)));
            dt.Columns.Add(new DataColumn("OrderProductCode", typeof(string)));
            dt.Columns.Add(new DataColumn("OrderProductName", typeof(string)));
            return dt;
        }
    }
}
